using Concurso;

namespace ConcursoCV.UI.WebUserControls.Jurado
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using Curriculum.Modelo.Clases;
	using Curriculum.Services;

	/// <summary>
	///		Summary description for ABMJurados.
	/// </summary>
	public class ABMJurados : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.TextBox txtApellido;
		protected System.Web.UI.WebControls.DropDownList ddlInstitucion;
		protected System.Web.UI.WebControls.Button btnAceptar;
		protected System.Web.UI.WebControls.Button btnCancelar;
		protected System.Web.UI.WebControls.TextBox txtNombre;
		protected System.Web.UI.WebControls.Label lblTitulo;
		protected System.Web.UI.WebControls.DropDownList ddlCargo;
		protected System.Web.UI.WebControls.TextBox txtObservaciones;
		protected System.Web.UI.WebControls.TextBox txtDireccion;
		protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
		protected System.Web.UI.WebControls.RequiredFieldValidator rfvApellido;
		protected System.Web.UI.WebControls.RequiredFieldValidator rfvNombre;
		
		private const string pidListadoDeJurados = "001";
		private PageMode mode;
		protected System.Web.UI.WebControls.CheckBox chkExterno;
		private string juradoId = null;

		private void Page_Load(object sender, System.EventArgs e)
		{
			lblTitulo.Text = "Nuevo Jurado";

			string id = this.Request["juradoId"];
			if ( id != null ) 
			{
				juradoId = id;			
			}

			string pMode = this.Request["PageMode"];
			mode = pMode != null ? PageModeConverter.ToEnum( pMode ) : PageMode.NEW;

			
			if (!this.IsPostBack) 
			{
				this.PopulateCombos();


				if ( mode == PageMode.EDIT || mode == PageMode.VIEW ) 
				{
					CargaJuradoAControles();
				}

				if ( mode == PageMode.EDIT )
				{
					lblTitulo.Text = "Modificar Jurado";
				}
				if ( mode == PageMode.VIEW ) 
				{
					lblTitulo.Text = "Ver Jurado";
					EnabledDisableControls(false);
				}
			}
		}

		private void EnabledDisableControls(bool enabled)
		{
			txtApellido.Enabled = enabled;
			txtDireccion.Enabled = enabled;
			txtNombre.Enabled = enabled;
			txtObservaciones.Enabled = enabled;
			ddlCargo.Enabled = enabled;			
			ddlInstitucion.Enabled = enabled;
			btnAceptar.Enabled = enabled;
		}

		private void PopulateCombos()
		{
			PopulateComboInstituciones();
			PopulateComboCargos();
		}

		private void PopulateComboInstituciones()
		{
			Helper.ListHelper.FillDropDownListLookup(ddlInstitucion,"INSTIT",false);
		}
		private void PopulateComboCargos()
		{
			Helper.ListHelper.FillDropDownListLookup(ddlCargo,"CARGO",false);
		}

		#region - Loads Methods -

		private void CargaJuradoAControles()
		{
			JuradoService svc = new JuradoService(); 
			
			Jurado jurado = svc.ObtenerJurado(juradoId);
			
			txtApellido.Text = jurado.Apellido;
			txtNombre.Text = jurado.Nombre;
			txtDireccion.Text = jurado.Domicilio;
			txtObservaciones.Text = jurado.Observaciones;
			ddlCargo.SelectedValue = jurado.Cargo.Id;
			ddlInstitucion.SelectedValue = jurado.Institucion.Id;
			chkExterno.Checked = jurado.EsExterno;
		}

		private void CargarDatosAJurado(Jurado jurado)
		{
			jurado.Id = juradoId;
			jurado.Institucion = (new LookupTypeService()).ObtenerLookup( ddlInstitucion.SelectedValue );
			jurado.Nombre = txtNombre.Text;
			jurado.Apellido = txtApellido.Text;
			jurado.Observaciones = txtObservaciones.Text;
			jurado.Domicilio = txtDireccion.Text;
			jurado.Cargo = (new LookupTypeService()).ObtenerLookup( ddlCargo.SelectedValue );
			jurado.EsExterno = chkExterno.Checked;
		}

		#endregion - Loads Methods -

		private void CrearJurado(Jurado jurado) 
		{
			JuradoService service = new JuradoService();
			juradoId = (string) service.CrearJurado(jurado);
		}

		private void ModificarJurado(Jurado jurado) 
		{
			JuradoService service = new JuradoService();
			juradoId = (string) service.ModificarJurado(jurado);		
		}
		private void SaveData()
		{
			if ( mode == PageMode.VIEW ) 
			{
				btnCancelar_Click(null, null);
				return;
			}

			Jurado jurado = new Jurado();
			CargarDatosAJurado(jurado);
			
			// UPDATE
			if ( mode == PageMode.EDIT ) 
			{
				ModificarJurado(jurado);
			}
			else
			{
				if( mode == PageMode.NEW )
				{
					// CREATE
					CrearJurado(jurado);
				}
			}								
			RedirectToListaDeJurados();			
		}

		private void RedirectToListaDeJurados()
		{
			this.Server.Transfer("Default.aspx?pid=" + pidListadoDeJurados);			
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnAceptar.Click += new System.EventHandler(this.btnAceptar_Click);
			this.btnCancelar.Click += new System.EventHandler(this.btnCancelar_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnCancelar_Click(object sender, System.EventArgs e)
		{
			RedirectToListaDeJurados();
		}

		private void btnAceptar_Click(object sender, System.EventArgs e)
		{
			SaveData();		
		}
	}
}
